/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { BuildDirConst } from '@ohos/hvigor-ohos-plugin/src/const/build-directory-const.js';
import { InjectUtil } from '@ohos/hvigor-ohos-plugin/src/utils/inject-util.js';
import { runHvigor } from '@ohos/hvigor-ohos-plugin/test/e2e-test/common/common.js';
import * as fs from 'fs';
import * as path from 'path';
import { basename, resolve } from 'path';

import {
  appRoot,
  initByProject,
  initExecuteCommand,
  runHvigorReturnResult
} from '../../base/src/common/common.js';
import { addModule } from '../src/common/common.js';

describe('test incremental execute.', () => {
  test('test incremental project execute successful', async () => {
    await initByProject('incremental-test', async (rootProject) => {
      const commands = initExecuteCommand({
        tasks: 'copy'
      });

      const result = await runHvigor(rootProject, commands);

      expect(result.stdout).toContain('Finished :module1:copy...');
      expect(result.stdout).toContain('BUILD SUCCESSFUL');

      const executeAgainResult = await runHvigor(rootProject, commands);
      expect(executeAgainResult.stdout).toContain('UP-TO-DATE :module1:copy...');
      expect(executeAgainResult.stdout).toContain('BUILD SUCCESSFUL');
      const customProjectBuildDir = resolve(InjectUtil.getBuildCacheParentDir(rootProject, basename(rootProject)),
        BuildDirConst.BUILD_ROOT);
      const outputFile = path.resolve(customProjectBuildDir, 'package.json');

      fs.rmSync(outputFile);

      const executeResultWithDeleteOutput = await runHvigor(rootProject, commands);
      expect(executeResultWithDeleteOutput.stdout).toContain('Finished :module1:copy...');
      expect(executeResultWithDeleteOutput.stdout).toContain('BUILD SUCCESSFUL');

    }, undefined, appRoot(__dirname));
  });

  test('test incremental task with --noIncremental', async () => {
    await initByProject('incremental-test', async (rootProject) => {
      const commands = initExecuteCommand({
        tasks: 'copy'
      });

      const result = await runHvigor(rootProject, commands);

      expect(result.stdout).toContain('Finished :module1:copy...');
      expect(result.stdout).toContain('BUILD SUCCESSFUL');

      const commandsWithNoIncremental = initExecuteCommand({
        tasks: 'copy',
        noIncremental: true
      });

      const executeResultWithNoIncremental = await runHvigor(rootProject, commandsWithNoIncremental);
      expect(executeResultWithNoIncremental.stdout).toContain('Finished :module1:copy...');
      expect(executeResultWithNoIncremental.stdout).toContain('BUILD SUCCESSFUL');

    }, undefined, appRoot(__dirname));
  });

  test('test incremental task input file must exist', async () => {
    await initByProject('incremental-test', async (rootProject) => {
      addModule(rootProject, 'module2');
      const commands = initExecuteCommand({
        tasks: 'copy'
      });
      const result2 = await runHvigorReturnResult(rootProject, commands);
      const errorPath = path.resolve(rootProject, 'error.json');
      expect(result2).toContain(`Failed to find the incremental input file: ${errorPath}`);

    }, undefined, appRoot(__dirname));
  });
});
